//
//  BuildingDetailViewController.m
//  Utils
//
//  Created by enghou on 2018/7/3.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "BuildingDetailViewController.h"

@interface BuildingDetailViewController ()
@property (weak, nonatomic) IBOutlet UITextField *buindingNameField;
@property (weak, nonatomic) IBOutlet UITextField *buildingStructureField;
@property(nonatomic,strong)PropertyDictItemModel *structureModel;
@property(nonatomic,strong)NSArray *structures;
@property (weak, nonatomic) IBOutlet UITextField *inresponseField;

@property (weak, nonatomic) IBOutlet UITextField *buildingTypeField;
@property(nonatomic,strong)NSArray *buildingTypes;
@property(nonatomic,strong)PropertyDictItemModel *buildingTypeModel;


@property (weak, nonatomic) IBOutlet UITextField *buildingAddressField;
@property (weak, nonatomic) IBOutlet UITextField *manipulatorField;
@property (weak, nonatomic) IBOutlet UITextField *manipulatorTelField;



@end

@implementation BuildingDetailViewController

-(instancetype)init{
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"BuildingDetail" bundle:[NSBundle mainBundle]];
    return [story instantiateInitialViewController];
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark - 获取详情
-(NSString *)detailPath{
    return @"/mobile/comprehensiveGovernance/builddatas/getBuilddatasById.action";
}

-(NSDictionary *)detailParam{
    return @{@"id":self.ids};
}

-(void(^)(NSDictionary *))configView{
    void(^config)(NSDictionary *) = ^(NSDictionary *source){
        NSDictionary *data = source[@"data"];
        self.buildingAddressField.text = data[@"buildingaddress"];
        self.buindingNameField.text = data[@"buildingname"];
        NSDictionary *buildingstruct = data[@"buildingstructures"];
        self.structureModel = [PropertyDictItemModel new];
        self.structureModel.ids = buildingstruct[@"id"];
        self.buildingStructureField.text = buildingstruct[@"displayName"];
        self.manipulatorTelField.text = data[@"phone"];
        self.manipulatorField.text = data[@"responsibleperson"];
        self.inresponseField.text = data[@"owner"];
        NSDictionary *type = data[@"type"];
        self.buildingTypeModel = [PropertyDictItemModel new];
        self.buildingTypeModel.ids = type[@"id"];
        self.buildingTypeField.text = type[@"displayName"];
    };
    return config;
}

#pragma 更新记录
-(NSString *)updatePath{
    return @"/mobile/comprehensiveGovernance/builddatas/updateBuilddatas.action";
}

-(NSDictionary *)updateParameter{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    if (self.ids) {
        param[@"builddatas.id"] = self.ids;
    }
    if (self.buindingNameField.text.length > 0) {
        param[@"builddatas.buildingname"] = self.buindingNameField.text;
    }else{
        [self alertSome:@"请输入楼宇名称"];
        return nil;
    }
    if (self.structureModel.ids.length > 0) {
        param[@"builddatas.buildingstructures.id"] = self.structureModel.ids;
    }
    if (self.buildingTypeModel.ids.length > 0) {
        param[@"builddatas.type.id"] = self.buildingTypeModel.ids;
    }
    if (self.buildingAddressField.text.length > 0) {
        param[@"builddatas.buildingaddress"] = self.buildingAddressField.text;
    }
    if (self.inresponseField.text.length > 0) {
        param[@"builddatas.owner"] = self.inresponseField.text;
    }
    if (self.manipulatorField.text.length > 0) {
        param[@"builddatas.responsibleperson"] = self.manipulatorField.text;
    }
    if (self.manipulatorTelField.text.length > 0) {
        param[@"builddatas.phone"] = self.manipulatorTelField.text;
    }
    
    
    return param;
}

//新增记录
-(NSString *)addPath{
    return @"/mobile/comprehensiveGovernance/builddatas/addBuilddatas.action";
}

-(NSDictionary *)addedParameter{
    return [self updateParameter];
}

//删除记录
-(NSString *)delPath{
    return @"/mobile/comprehensiveGovernance/builddatas/deleteBuilddatasById.action";
}

-(NSDictionary *)delParameter{
    return @{@"id":self.ids};
}

//操作完成后发送的通知
-(NSString *)refresh_not_name{
    return @"RefreshLst";
}

-(void)chooseStructure{
    [self commonAlertWithContent:@"出租房结构" choosenBlock:^(PropertyDictItemModel *model) {
        self.structureModel = model;
        self.buildingStructureField.text = self.structureModel.displayName;
    } getList:^(NSArray *list) {
        self.structures = list;
    }];
}

-(void)chooseType{
    [self commonAlertWithContent:@"楼宇类型" choosenBlock:^(PropertyDictItemModel *model) {
        self.buildingTypeModel = model;
        self.buildingTypeField.text = model.displayName;
    } getList:^(NSArray *list) {
        self.buildingTypes = list;
    }];
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 1:
            [self chooseStructure];
            break;
        case 2:
            [self chooseType];
            break;
        default:
            break;
    }
}



@end
